package com.lagou.concurrent.demo;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;

public class CompletableFutureDemo9 {

    private static final Random RANDOM = new Random();
    private static volatile int result = 0;

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture[] futures = new CompletableFuture[10];

        for (int i = 0; i < 10; i++) {
            CompletableFuture<Void> myFuture = CompletableFuture.runAsync(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000 + RANDOM.nextInt(1000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    result++;
                }
            });

            futures[i] = myFuture;
        }

//        for (int i = 0; i < 10; i++) {
//            futures[i].get();
//            System.out.println(result);
//        }

//        CompletableFuture<Void> future = CompletableFuture.allOf(futures).thenRun(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("计算完成");
//            }
//        });
//
//        future.get();
//        System.out.println(result);


        CompletableFuture myfuture = CompletableFuture.anyOf(futures).thenRun(new Runnable() {
            @Override
            public void run() {
                System.out.println(result);
            }
        });

        myfuture.get();
    }
}
